package com.app.lxx.pregnancyberth.utils;

import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Describe: 和服务器进行连接的网络类
 */
public class HttpUtil {
    public static void sendOkHttpRequest(String address, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(address).build();
        client.newCall(request).enqueue(callback);
    }

    //java发送raw请求代码如下：
//    public static JSONObject deviceRequest() {
//
//        JSONObject result = null;
//
//        try {
//            String url = "url 地址";
//            JSONObject json = new JSONObject();
//            json.put("param", "param 参数");
//            @SuppressWarnings({"resource"})
//            HttpClient httpClient = new DefaultHttpClient();
//            HttpPost post = new HttpPost(url);
//            StringEntity postingString = new StringEntity(json.toJSONString());
//            post.setEntity(postingString);
//            post.setHeader("Content-type", "application/json");
//            HttpResponse response = httpClient.execute(post);
//            String content = EntityUtils.toString(response.getEntity());
//
//            result = (JSONObject) JSONObject.parse(content);
//
//            System.out.println(result);
//
//        } catch (ParseException | IOException e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }

    // java获取raw包请求需要从流中获取：
//    public static String readRaw(InputStream inputStream) {
//
//        String result = "";
//        try {
//            ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
//            byte[] buffer = new byte[1024];
//
//            int len;
//            while ((len = inputStream.read(buffer)) != -1) {
//                outSteam.write(buffer, 0, len);
//            }
//
//            outSteam.close();
//            inputStream.close();
//
//            result = new String(outSteam.toByteArray(), "UTF-8");
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }
}
